
var isjump = 0;//判断是否跳转

var color_page; //(参数名字不要取一样！)
var username;
var secretkey;

layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
	if(window.sessionStorage.getItem("color_page")){
 		var data = JSON.parse(window.sessionStorage.getItem("color_page"));
		pageNo = data.pageNo;
 	}
	find_allcolor(pageRecords,pageNo);
});

//搜索查询用户
function search(){
	isjump=0;
	find_allcolor(pageRecords,pageNo);
}

function find_allcolor(pageRecords,pageNo){
	color_page='{"pageNo":"'+pageNo+'"}';
//	console.log(color_page);
	window.sessionStorage.setItem("color_page",color_page);
//    console.log(username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + xiyitype + "','" + goods_name);
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "g_xmy.find_allcolor",
			para:"['" + username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "']"
		},
		dataType: "json",
		success: function(data){
		//	console.log(JSON.stringify(data));
			var tab=document.getElementById("tableid1");
			var ro=tab.rows.length;
			for(var b=1;;){
				if(tab.rows.length==1){
					break;
				}
				tab.deleteRow(b);
			}
			if (data[0].TAG == "0" ){
				layer.alert(data[0].MSG);
				top.location.href="../../login.html"
			}else{

				for(var a=0;a<data.length;a++){
					if(a==data.length-1){
						totalRecords = data[a].ID;
						totalPage = data[a].C_NAME;
						if(isjump ==0){
							page();
							isjump = 1;
						}
						return;
					}
					var tables=document.getElementById("tableid1");
					var ro=tables.insertRow(a+1);
					cell1=ro.insertCell(0);
					cell1.innerHTML=data[a].ID;
					cell1.align="center";
					cell2=ro.insertCell(1);
					cell2.innerHTML=data[a].C_NAME;
					cell2.align="center";
					cell3=ro.insertCell(2);
					cell3.innerHTML=data[a].BRIEF_CODE;
					cell3.align="center";
					cell4=ro.insertCell(3);
					cell4.innerHTML='<a href="#" onclick="up_classification('+data[a].ID+')">编辑</a>&nbsp;&nbsp;<a href="#" onclick="delete_classification('+data[a].ID+')">删除</a>';
					cell4.align="center";

				}
			}
		}
	});
}
//分页
function page(){
	laypage.render({
		 elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
		//	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
		//	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  find_allcolor(obj.limit,obj.curr);
			}
		  }
	});
}


//点击查看跳转页面
function up_classification(id){
	$.session.set("color_id",id);
	window.location.href="./up_color.html";
}

//删除
function delete_classification(id){
	var username = $.session.get("username");
	var secretkey = $.session.get("user_secretkey");
	layer.confirm('确定删除该颜色吗？', {
		  title:'系统提示',
		  btn: ['确定','取消']
		}, function(){
			 $.ajax({
				url: "../../../../service",
				data : {
					proc : "g_xmy.delete_color",
					para : "['" + username + "','" + secretkey + "','" + id + "']"
				},
				dataType : "json",
				success : function(data) {
					if (data[0].TAG == "1") {
						layer.alert("成功删除该颜色!",function(index){
							 window.location.href="./color_gement.html";
							 layer.close(index);
						});
					}else{
						layer.alert("删除失败!");
					//	window.location.href="./error.html";
					}
				}
			});
		});

}
